﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using Ext.Net;
using Sharp.Infrastructure;
using Sharp.Platform;
using Sharp.Platform.List;
using Sharp.Platform.Other;

namespace Sharp.Web
{
    /// <summary>
    /// 工具栏代码具体实现
    /// </summary>
    public class ExtToolButtons : Sharp.Platform.IToolButton
    {

        public string DetailsButton(ToolButtonInfo config)
        {
            return
               string.Format("x.Button().Icon(Icon.ApplicationForm).Text(\"{0}\").OnClientClick(\"App.Grid.OnModuleOperation(this,'\" + Url.Action(\"Details\") + \"');\")", config.Title);

        }

        /// <summary>
        /// 新增
        /// </summary>
        /// <returns></returns>
        public string CreateButton(ToolButtonInfo config)
        {
            return
                "x.Button().Icon(Icon.Add).Text(\"新增\").OnClientClick(\"App.Grid.OnModuleOperation(this,'\" + Url.Action(\"Create\") + \"',{ShowOnly:true});\")";
        }



        public string EditButton(ToolButtonInfo config)
        {
            return
                "x.Button().Icon(Icon.ApplicationFormEdit).Text(\"修改\").OnClientClick(\"App.Grid.OnModuleOperation(this,'\" + Url.Action(\"Edit\") + \"');\")";
        }

        public string DeleteButton(ToolButtonInfo config)
        {
            return
                "x.Button().Icon(Icon.Delete).Text(\"删除\").OnClientClick(\"App.Grid.OnModuleOperation(this,'\" + Url.Action(\"Delete\") + \"',{IsAjax:true});\")";
        }

        public string RefreshButton(ToolButtonInfo config)
        {
            throw new NotImplementedException();
        }

        /// <summary>
        /// 自定义按钮
        /// </summary>
        /// <param name="config"></param>
        /// <returns></returns>
        public string CustomButton(ToolButtonInfo config)
        {
            StringBuilder sb = new StringBuilder("x.Button()");
            sb.AppendFormat(".Text(\"{0}\")", config.Title);
            if (!config.Icon.IsNullOrEmpty())
            {
                sb.AppendFormat(".Icon({0})", config.Icon);
            }
            if (!config.OnClientClick.IsNullOrEmpty())
            {
                sb.AppendFormat(".OnClientClick(\"{0}\")", config.OnClientClick);
            }
            if (config.Direct != null)
            {
                sb.AppendFormat(".DirectClickAction(\"{0}\", \"{1}\")", config.Direct.ActionName, config.Direct.ControllerName);
            }
            if (config.Parameters != null && config.Parameters.Any())
            {
                sb.AppendLine(".DirectEvents(de =>");
                sb.AppendLine("{");
                foreach (var p in config.Parameters)
                {

                    var pm = (ParameterMode)p.Mode;
                    if (pm == ParameterMode.Auto)
                    {
                        sb.AppendFormat("de.Click.ExtraParams.Add(new Parameter(\"{0}\", \"{1}\"));\r\n", p.Name,
                            p.Value);
                    }
                    else
                    {
                        sb.AppendFormat("de.Click.ExtraParams.Add(new Parameter(\"{0}\", \"{1}\", ParameterMode.Raw));\r\n", p.Name,
                            p.Value);
                    }
                }
                sb.AppendLine("})");
            }

            return sb.ToString();
        }
    }
}